$(function(){
    $('#login-box a').on('click',function(){
        $('#login-box').hide()
        $('#reg-box').show()
    })
    $('#reg-box a').on('click',function(){
        $('#login-box').show()
        $('#reg-box').hide()
    })


    let form = layui.form
    form.verify({
        username: [
            /^[a-zA-Z0-9]{1,10}$/,
            '用户名必须是1-10位字母和数字'
        ],
        password: [
            /^[\S]{6,15}$/,
            '密码长度必须是6-15位的非空字符串'
        ],
        rePassword: function(value) {
            let pwd = $('#reg-box [name=password]').val()
            if(value !== pwd) {
                return '两次密码不一致！'
            }
        }
    })

    $('#fromReg').on('submit',function(e){
        e.preventDefault()
        axios({
            method: 'POST',
            url: '/api/reguser',
            data: $(this).serialize()
        }).then(({ data: res }) => {
            if(res.status !== 0) {
                return layui.layer.msg(res.message)
            }
            layui.layer.msg('恭喜您，注册成功！')
            $('#reg-box a').click()
            $('#fromReg')[0].reset()
        })
    })

    $('#formLogin').on('submit',function(e){
        e.preventDefault()
        axios({
            method: 'POST',
            url: '/api/login',
            data: $(this).serialize()
        }).then(({ data: res }) => {
            console.log(res);
            if(res.status !== 0) {
                return layui.layer.msg(res.message)
            }
            layui.layer.msg('恭喜您，登录成功！')
            location.href = '/index.html'
            localStorage.setItem('token',res.token)
        })
    })















})